﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;

namespace BaiduAPI
{
    public class WebInvoke<T> where T:class ,new ()
    {
        public static T Invoke(string strRequestAddress)
        {
            try
            {
                System.Net.WebRequest wrq = System.Net.WebRequest.Create(strRequestAddress);
                wrq.Method = "GET";
                System.Net.WebResponse wrp = wrq.GetResponse();
                System.IO.StreamReader sr = new System.IO.StreamReader(wrp.GetResponseStream(),
                    System.Text.Encoding.Default);
                string strResult = sr.ReadToEnd();
                TA<T> ta = null;
                try
                {
                    ta = JsonConvert.DeserializeObject<TA<T>>(strResult);
                }
                catch
                {
                }
                if (ta == null)
                {
                    return null;
                }
                else
                {
                    //表示成功
                    if (ta.errNum == 0)
                    {
                        return ta.retData;
                    }
                    else
                    {
                        return null;
                    }
                }
            }
            catch
            {
                return null;
            }

        }
    }
}
